package com.banciyuan.bcywebview.base.helper;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;

/**
 * Created by hy on 2015/7/20.
 */
public class LoadingHelper implements View.OnClickListener {
    public static final int PROGRESS_STATE_SUCCESS = 1001;
    public static final int PROGRESS_STATE_FAILED = 1002;
    public static final int PROGRESS_STATE_LOADING = 1003;
    public static final int PROGRESS_STATE_NODATA = 1004;

    private LinearLayout mLoadingLayout;
    private LinearLayout mFailedLayout;
    private RelativeLayout rlEmptyView;
    private LinearLayout mCustomLayout;
    private TextView mFailedTv;

    private OnProgressCallbacks mCallbacks;

    private int stateNumber = PROGRESS_STATE_SUCCESS;

    public LoadingHelper(View mLoadingrView) {

        mLoadingLayout = (LinearLayout) mLoadingrView.findViewById(R.id.progressbar_loading_layout);
        mFailedLayout = (LinearLayout) mLoadingrView.findViewById(R.id.progressbar_failed_layout);
        mFailedTv = (TextView) mLoadingrView.findViewById(R.id.progressbar_failed_tv);
        rlEmptyView = (RelativeLayout) mLoadingrView.findViewById(R.id.rl_empty_view);
        mCustomLayout = (LinearLayout) mLoadingrView.findViewById(R.id.custom_container);

        mFailedLayout.setOnClickListener(this);
        mFailedTv.setOnClickListener(this);
        rlEmptyView.setOnClickListener(this);
    }

    public LoadingHelper(View mLoadingrView, View emptyView) {

        mLoadingLayout = (LinearLayout) mLoadingrView.findViewById(R.id.progressbar_loading_layout);
        mFailedLayout = (LinearLayout) mLoadingrView.findViewById(R.id.progressbar_failed_layout);
        mFailedTv = (TextView) mLoadingrView.findViewById(R.id.progressbar_failed_tv);
        mCustomLayout = (LinearLayout) mLoadingrView.findViewById(R.id.custom_container);

        this.rlEmptyView = (RelativeLayout) emptyView;
        mFailedLayout.setOnClickListener(this);
        mFailedTv.setOnClickListener(this);
        rlEmptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callAction(view.getId());
            }
        });
    }

    public void onFailed() {
        if (getState() != PROGRESS_STATE_LOADING) {
            stateNumber = PROGRESS_STATE_FAILED;
        } else {
            stateNumber = PROGRESS_STATE_FAILED;
            mLoadingLayout.setVisibility(View.GONE);
            rlEmptyView.setVisibility(View.GONE);
            mFailedLayout.setVisibility(View.VISIBLE);
        }
    }

    public void onFailed(String onfailedTx) {
        if (getState() != PROGRESS_STATE_LOADING) {
            stateNumber = PROGRESS_STATE_FAILED;
            mFailedTv.setText(onfailedTx);
        } else {
            stateNumber = PROGRESS_STATE_FAILED;
            mLoadingLayout.setVisibility(View.GONE);
            rlEmptyView.setVisibility(View.GONE);
            mFailedLayout.setVisibility(View.VISIBLE);
            mFailedTv.setText(onfailedTx);
        }
    }

    public void onLoading() {
        stateNumber = PROGRESS_STATE_LOADING;
        mFailedLayout.setVisibility(View.GONE);
        rlEmptyView.setVisibility(View.GONE);
        mLoadingLayout.setVisibility(View.VISIBLE);
    }

    public void onNodata() {
        if (getState() != PROGRESS_STATE_LOADING) {
            stateNumber = PROGRESS_STATE_NODATA;
        } else {
            stateNumber = PROGRESS_STATE_NODATA;
            mFailedLayout.setVisibility(View.GONE);
            mLoadingLayout.setVisibility(View.GONE);
            rlEmptyView.setVisibility(View.VISIBLE);
        }
    }

    public void onNodata(String nodataTx) {
        if (getState() != PROGRESS_STATE_LOADING) {
            stateNumber = PROGRESS_STATE_NODATA;
        } else {
            stateNumber = PROGRESS_STATE_NODATA;
            mFailedLayout.setVisibility(View.GONE);
            mLoadingLayout.setVisibility(View.GONE);
            rlEmptyView.setVisibility(View.VISIBLE);
        }
    }

    public void onNodata(String descOne, String descTwo) {
        if (getState() != PROGRESS_STATE_LOADING) {
            stateNumber = PROGRESS_STATE_NODATA;
        } else {
            stateNumber = PROGRESS_STATE_NODATA;
            mFailedLayout.setVisibility(View.GONE);
            mLoadingLayout.setVisibility(View.GONE);
            rlEmptyView.setVisibility(View.VISIBLE);
        }
    }

    public void onNodata(String nodataTx, boolean setNoDataVisible) {
        if (setNoDataVisible) {
            stateNumber = PROGRESS_STATE_NODATA;
            mFailedLayout.setVisibility(View.GONE);
            mLoadingLayout.setVisibility(View.GONE);
            rlEmptyView.setVisibility(View.VISIBLE);
        }
    }

    public void onSuccess() {
        stateNumber = PROGRESS_STATE_SUCCESS;
        mFailedLayout.setVisibility(View.GONE);
        mLoadingLayout.setVisibility(View.GONE);
        rlEmptyView.setVisibility(View.GONE);
    }

    public void onCustom() {
        mFailedLayout.setVisibility(View.GONE);
        mLoadingLayout.setVisibility(View.GONE);
        rlEmptyView.setVisibility(View.GONE);
        mCustomLayout.setVisibility(View.VISIBLE);
    }

    public LinearLayout getmCustomLayout() {
        return mCustomLayout;
    }

    public int getState() {
        return stateNumber;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.progressbar_failed_layout:
            case R.id.progressbar_nodata_layout:
            case R.id.progressbar_failed_tv:
            case R.id.progressbar_nodata_tv:
            case R.id.rl_empty_view:
                callAction(v.getId());
                break;

            default:
                break;
        }
    }

    private void callAction(int id) {
        if (mCallbacks != null) {
            mCallbacks.onActionClick(id);
        }
    }

    public void setActionCallbacks(OnProgressCallbacks callbacks) {
        mCallbacks = callbacks;
    }

    public interface OnProgressCallbacks {
        void onActionClick(int id);
    }

}
